package com.hertz.alarm.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class CollectionUtil{

	public static <K, V> List<V> toList(Map<K, V> map, Collection<K> order) {
        List<V> result = new ArrayList<V>(order.size());
        for (K key : order) {
            result.add(map.get(key));
        }
        return result;
    }
	
	public static byte mapByte(Map<?,?> map,String prop){
		Object obj =  map.get(prop);
		return obj == null ? 0 : (Byte)obj;
	}
	
	public static long mapLong(Map<?,?> map,String prop){
		Object obj =  map.get(prop);
		return obj == null ? 0 : (Long)obj;
	}
	
	public static String mapString(Map<?,?> map,String prop){
		Object obj =  map.get(prop);
		return obj == null ? "" : obj.toString();
	}
	
	public static List<Long> collectionToList(Collection<?> coll){
		return collectionToList(coll,0,-1);
	}
	
	public static List<String> collectionToStrList(Collection<?> coll,int start,int len){
		if(coll == null || coll.isEmpty()) return Collections.emptyList();
		List<String> ret = new ArrayList<String>(coll.size());
		len = len==-1?coll.size():len;
		int i=0;
		for(Object obj:coll){
			if(i++<start) continue;			
			ret.add(obj.toString());
			if(i>=(start+len)) break;
		}
		return ret;
	}
	
	public static List<Long> collectionToList(Collection<?> coll,int start,int len){
		if(coll == null || coll.isEmpty()) return Collections.emptyList();
		List<Long> ret = new ArrayList<Long>(coll.size());
		len = len==-1?coll.size():len;
		int i=0;
		for(Object obj:coll){
			if(i++<start) continue;			
			ret.add(Long.parseLong(obj.toString()));
			if(i>=(start+len)) break;
		}
		return ret;
	}
	
	public static List<Long> listToList(List<String> list){
		return listToList(list,0,-1);
	}
	
	public static List<Long> listToList(List<String> list,int start,int len){
		return collectionToList(list,start,len);
	}
	
	public static List<Long> setToList(Set<String> set,int start,int len){
		return collectionToList(set,start,len);
	}
 
	public static List<Long> setToList(Set<String> set){
		return collectionToList(set,0,-1);
	}
	
	/*public static List<Long> setToList(Collection fset,Collection set,int start,int len){
		start = start<0?0:start;
		List<Long> list = collectionToList(fset,start,len);
		if(list==null){
			return collectionToList(set,start,len);
		}		
		if(list.size()<len){
			if(list.isEmpty()){
				start = start - fset.size();
			}else{
				start = 0;
				len = len - list.size();
			}
			list.addAll(collectionToList(set,start,len));
		}
		return list;
	}*/
	
	public static <T> List<T>  subList(List<T> list,int start,int len){
		if(list==null) return null;		
		if(start>list.size()) return null;
		int t = len==-1?list.size():Math.min((start+len), list.size());
		return list.subList(start, t);
	}
	
	public static String arraysToStr(Object[] objs,String split){
		if(objs==null || objs.length == 0) return "";
		StringBuffer sb = new StringBuffer();		
		for(Object obj:objs){
			sb.append(obj.toString()).append(split);
		}
		sb.deleteCharAt(sb.length()-split.length());
		return sb.toString();
	}
	
	public static String collectionToStr(Collection<?> coll,String split){
		if(coll==null || coll.isEmpty()) return "";
		StringBuffer sb = new StringBuffer();		
		for(Object obj:coll){
			sb.append(obj.toString()).append(split);
		}
		sb.deleteCharAt(sb.length()-split.length());
		return sb.toString();
	}
	
	public static List<Long> longStrToList(String lStr,String split){
		 if(lStr==null || "".equals(lStr)) return null;
		 String arr[] = lStr.split(split);
		 List<Long> list = new ArrayList<Long>();
		 for(String tmp:arr){
			 if("".equals(tmp)) continue;
			 if(org.apache.commons.lang.StringUtils.isNumeric(tmp)){
				 list.add(Long.parseLong(tmp));
			 }
		 }
		 return list;
	}
	
	public static Set<Long> longStrToSet(String lStr,String split){
		 if(lStr==null || "".equals(lStr)) return null;
		 String arr[] = lStr.split(split);
		 Set<Long> set = new TreeSet<Long>();
		 for(String tmp:arr){
			 if("".equals(tmp)) continue;
			 set.add(Long.parseLong(tmp));
		 }
		 return set;
	}

}
